$(function(){
'use strict';
//登陆ajax
$(document).on('click', '#login_but', function(e){
      var user_tel = $("#user_tel").val();
      var password = $("#password").val();
      if(!user_tel || !password) { $.toast("账号或者密码不可以为空!");  return false; }
      $.ajax({
        type: 'POST',
        url: login_url,
        data: { user_tel:user_tel,password:password },
        dataType: 'json',
        timeout: 2000,
        success: function(data){
          if(data.status==0){
             $.toast(data.info);
          }else{ 
             $.toast(data.info);
             setTimeout(function(){
                t_url(data.url);
             },2000);
          }
        },
      error: function(xhr, type){$.toast("服务器抽风了,请稍等一会在试!"); },
      beforeSend:function(){$("#reg_sub").attr('disabled',true);}, //禁止
      complete:function(xhr,status){
        setTimeout(function(){
          $("#reg_sub").attr('disabled',null);
        },2000);
      }
    });
});

$(document).on("click","#tc_news",function(){
   $("#tc_news").hide();
});
$(document).on("click","#tc_shop",function(){
   $("#tc_shop").hide();
});


//新闻无效滚动
$(document).on("pageInit", "#news-list-infinite-scroll", function(e, id, page) {
    var loading = false;
    var pages=1;
    var tip='';
    //预先加载
    function dingzhi(hot){
      if(hot==1){
        return "[顶置]";
      }else{
        return '';
      }
    }
    addNews(pages);
    function price(price,cname)
    {
       if(price){
         return "阅读:"+price+"元";
       }else{
         return "来源:"+cname;
       }
    }
	function ticheng(item_id,suk,click)
	{ 
	  if(item_id!=='0' && suk){
		 return "<span class='right red'>&nbsp;&nbsp;&nbsp;&nbsp;分成:"+suk['min']+"-"+suk['max']+"</span>";
	   }else{
		 return "<span class='right'>&nbsp;&nbsp;&nbsp;&nbsp;点击:"+click+"</span>"; 
	   }
    }
    function addNews(page){
      $.ajax({
          type: 'GET',
          url: news_url,
          data: {'page': page-1},
          dataType: 'json',
          timeout: 2000,
          success:function(json){
             var total_num = json.total_num,//总记录数
                 page_size = json.page_size,//每页数量
                 page_cur = page,//当前页
                 page_total_num = json.page_total_num;//总页数
             var list = json.newslist;
             var html="";
             if(json.newslist==''){
                tip=true; 
                $(".infinite-scroll-preloader").html("已经没有更多内容了!");
             }
             $.each(list, function(index, array) { //遍历返回json
              html+='<div class="news wp">\
                    <a href="'+array['url']+'"  external>\
                    <div class="infobar">\
                    <div class="ti"><span class="red">'+dingzhi(array['hot'])+'</span>'+array['title']+'</div>\
                    <div class="time" ><span class="left">'+array['time']+'</span><span class="left red">&nbsp;&nbsp;&nbsp;&nbsp;'+price(array['price'],array['cname'])+'</span>'+ticheng(array['item_id'],array['suk'],array['click'])+'</div>\
                    </div>\
				    <div class="picbar"><img style="height:auto;" src="'+array['litpic']+'"></div>\
                    </a>\
                    </div>';
              });
            $(".news_list").append(html);
             pages = pages+1;
          },
          
      });
    }
$(page).on('infinite', function() {
      // 如果正在加载，则退出
      if (loading || tip) return;
      // 设置flag
      loading = true;
      // 模拟1s的加载过程
      setTimeout(function() {
        // 重置加载flag
        loading = false;
        addNews(pages);
        $.refreshScroller();
      }, 1000);
    });

 });


//提现记录ajax
$(document).on("pageInit", "#tixian-list-infinite-scroll", function(e, id, page) {
	var loading = false;
	var pages=1;
	var tip='';
	addTixian(pages);
    function addTixian(page){
      $.ajax({
          type: 'GET',
          url:   alipay_list_url,
          data: {'page': page-1},
          dataType: 'json',
          success:function(json){
             var total_num = json.total_num,//总记录数
                 page_size = json.page_size,//每页数量
                 page_cur = page,//当前页
                 page_total_num = json.page_total_num;//总页数
             var list = json.list;
             var html="";
             if(json.list==''){
                tip=true; 
                $("#tixian-list-infinite-scroll .infinite-scroll-preloader").html("已经没有更多内容了!");
             }
			 if(json.total_num<10){
			    $("#tixian-list-infinite-scroll .infinite-scroll-preloader").hide();  
			 }
             $.each(list, function(index, array) { //遍历返回json
              html+='<div class=" row">\
					<div class="col-50 f10">'+get_time(array['time'])+'</div>\
					<div class="col-20 f10">'+get_type(array['type'])+'</div>\
					<div class="col-15 f10">'+array['price']+'</div>\
					<div class="col-15 f10">'+get_status(array['status'])+'</div>\
                    </div>';
              });
            $("#alipay_list").append(html);
             pages = pages+1;
          },
          
      });
    }	
	$(page).on('infinite', function() {
      // 如果正在加载，则退出
      if (loading || tip) return;
      // 设置flag
      loading = true;
      // 模拟1s的加载过程
      setTimeout(function() {
        // 重置加载flag
        loading = false;
        addTixian(pages);
        $.refreshScroller();
      }, 1000);
    });

 });


$(document).on("pageInit", "#reg", function(e, id, page) {
 $(".txt-phone").bind("input",function() {
        var tel= $(this).val();
        if(isPhoneNum(tel)){
          $("#endsms").removeClass("btn-retransmit-disabled");
        }
    });
});




//24小时收益报告AJAX
$(document).on("pageInit","#share_list",function(e, id, page){
	var loading = false;
	var pages = 1;
	var tip = '';
	addshare(pages);
	function addshare(page){
      $.ajax({
          type:'GET',
          url:share_list_url,
          data:{'page': page-1},
          dataType: 'json',
          success:function(json){
             var total_num = json.total_num,//总记录数
                 page_size = json.page_size,//每页数量
                 page_cur = page,//当前页
                 page_total_num = json.page_total_num;//总页数
             var list = json.list;
             var html = "";
             if(json.list==''){
                 tip=true; 
                 $("#share_list .infinite-scroll-preloader").html("已经没有更多内容了!");
             }
			 if(json.total_num<10){
			     $("#share_list .infinite-scroll-preloader").hide();  
			 }
             $.each(list, function(index, array) { //遍历返回json
              html+='<div class="card-header row">\
					<div class="col-60 f10">'+array['news']['title'].substr(0,12)+'</div>\
					<div class="col-20 f10">'+array['count']+'</div>\
					<div class="col-20 f10">'+(array['count']*0.06).toFixed(2)+'</div>\
                    </div>';
              });
            $("#_share_list").append(html);
             pages = pages+1;
          },
        });		 
    }
	$(page).on('infinite',function(){
       if (loading || tip) return;
       loading = true;
       setTimeout(function(){
        loading = false;
        addshare(pages);
        $.refreshScroller();
      }, 1000);
   });      	 
});


$(document).on('click',".tcc",function(){
    $(".tcc").remove();
});


$.init();
});

   //邀请好友
  function fx_but_show()
  {
    $("#tcc").show();
  }
   //邀请好友 
  function tcc_hide()
  {
    $("#tcc").hide();
  }
  //文章分类菜单
  function news_nav()
  {
    $(".navmore").toggle();
  }



//微信客服
function yz_layer(id,img)
{
   
    $('body').append("<div id='"+id+"'' class='tcc'><img src='"+img+"'><div>");
    $("#"+id).show();
}




  //注册ajax
function reg_sub_ajax(){
    var n = $("#user_tel").val(),n2=$("#user_tel2").val(), n3=$("#code").val(),t = $("#password").val(),i = {user_tel: n,password: t};
    if(document.getElementById("user_tel2")){ i.user_tel2=n2;}
    if(document.getElementById("code")){i.code=n3;}
    if(!n || !t){ $.toast("账号或者密码不可以为空!");  return false; }
    $.ajax({ 
      url: reg_url,
      type: "POST",
      data: i,
      dataType: "json",
      timeout: 2000,
      success: function(data){
        if(data.status==0){ 
          $.toast(data.info);
        }else{ 
          $.toast(data.info);
          setTimeout(function(){
                t_url(data.url);
          },2000);
        }
      },
      error: function(xhr, type){$.toast("服务器抽风了,请稍等一会在试!"); },
      beforeSend:function(){$("#reg_sub").attr('disabled',true);}, //禁止
      complete:function(xhr,status){
        setTimeout(function(){
          $("#reg_sub").attr('disabled',null);
        },2000);
      }
});}


//安全效验
function xiaoyan_sub_ajax()
{
    var n = $("#user_tel").val(),n2=$("#user_tel2").val(), n3=$("#code").val(),t = $("#password").val(),i = {user_tel: n,password: t};
    if(document.getElementById("user_tel2")){ i.user_tel2=n2;}
    if(document.getElementById("code")){i.code=n3;}
    if(!n || !t){ $.toast("账号或者密码不可以为空!");  return false; }
    $.ajax({ 
      url: xiaoyan_url,
      type: "POST",
      data: i,
      dataType: "json",
      timeout: 2000,
      success: function(data){
        if(data.status==0){ 
          $.toast(data.info);
        }else{ 
          $.toast(data.info);
          setTimeout(function(){
                t_url(data.url);
          },2000);
        }
      },
      error: function(xhr, type){$.toast("服务器抽风了,请稍等一会在试!"); },
      beforeSend:function(){$("#reg_sub").attr('disabled',true);}, //禁止
      complete:function(xhr,status){
        setTimeout(function(){
          $("#reg_sub").attr('disabled',null);
        },2000);
      }
});}







//申请提现
$(document).on('click',"#aliapy_sub",function()
    {   
        var alipay    = $(".alipay").val();
        var alipayname= $(".alipayname").val();
        var price     = $(".price").val();
        var balance   =$("#balance").html();
        var b= balance-price;
        var i={
            alipay:alipay,
            alipayname:alipayname,
            price:price
        };
       if(!isAlipay(alipay)){
            $(".alipay").focus().addClass("error");
            $.toast("请输入正确的支付宝账号.");
            return false;
        }
        if(!isName(alipayname)){
            $(".alipayname").focus().addClass("error");
            $.toast("请输入正确的支付宝姓名.");
            return false;
        }
       if(!price){
          $(".price").focus().addClass("error");
            $.toast("请输入提现金额.");
          return false;
       }
       $.ajax({
            url: alipay_url,
            type: "POST",
            data: i,
            dataType: "json",
            success: function(json){
              if(json.status){
                var price     = $(".price").val();
                var balance   =$("#balance").html();
                var b= balance-price;
                $("#balance").html(b);
                $(".alipay").attr("disabled",true); 
                $(".alipayname").attr("disabled",true); 
                $(".price").val("");
              } 
               $.toast(json.info);
            },
			error: function(xhr, type){$.toast("服务器抽风了,请稍等一会在试!"); },
			beforeSend:function(){$("#reg_sub").attr('disabled',true);}, //禁止
			complete:function(xhr,status){
			setTimeout(function(){
			$("#reg_sub").attr('disabled',null);
			},2000);
			}
        });
});



//跳转
function t_url(url){ 
  location.href=url;
}


//获取提现类型
function get_type(type)
{
  if(type==1){
    return "支付宝";
  }else{
    return "话费充值";
  }
}

//获取提现状态
function get_status(status){
   if(status==1){
      return "处理中";
   }else if (status==2) {
      return "失败";
   }else if (status==3){
      return "拒绝";
   }else if (status==4){
      return "成功";
   }else{
     return "错误";
   }
}


//验证支付宝账号
function isAlipay(value) {
     if (!/(^(13[0-9]|15[0-9]|18[0-9]|14[0-9]|17[0-9])[0-9]{8}$)|(^([\w-\.]+)@([\w-]+\.)+([a-zA-Z]{2,4})$)/.test(value)) {
        return false;
    }else{
        return true;
    }
 }

 //验证姓名
 function isName(value)
 {
    if (!/^[\u4e00-\u9fa5]+$/.test(value) || value.length < 2 || value.length >5) {
        return false;
    }else{
        return true;
    }
 }
 
//验证提现金额
function isPrice(value)
{   
    if (!/^[1-9]{1}[0]{1}[0]{1}[0]*?$/.test(value)) {
        return false;
    }else{
        return true;
    }  
}


//时间戳格式化
function get_time(nS) {       
    var date = new Date(parseInt(nS) * 1000);
    return date.getFullYear()+"-"+(date.getMonth()+1)+"-"+date.getDate()+" "+date.toLocaleTimeString();     
}  



function doSendSmsCaptcha() {
    var n = $("#user_tel").val(),
    i = {
       mobile: n
    };
    isPhoneNum(n) && $.ajax({
        url: "/User/login/sms",
        type: "POST",
        data: i,
        dataType: "json",
        success: function(n) {
            var t,
            i,
            r;
            n = eval(n);
            if (n.code == 0) t = $(".btn-retransmit"),
            t.html("重新发送（60s）").addClass("btn-retransmit-disabled").addClass("is-restransit"),
            i = 60,

            r = setInterval(function() {--i;
                i == 0 ? (t.html("获取短信验证码").removeClass("btn-retransmit-disabled").removeClass("is-restransit"), clearInterval(r)) : t.html("重新发送（" + i + "s）")
            },
            1e3);
            else {
                alert(n.detail);
                $(".txt-phone").addClass("input-validation-error");
                return
            }
        },
        error: function() {
            $.toast("服务器抽风了,请稍等一会在试!");
            return
        }
    })
}

//校验手机号是否合法
function isPhoneNum(phonenum){
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/; 
    if(!myreg.test(phonenum)){ 
        return false; 
    }else{
        return true;
    }
}


$(document).on('click','#endsms',function() {
    $(this).hasClass("btn-retransmit-disabled") || $(this).hasClass("is-restransit") || ($(this).addClass("btn-retransmit-disabled"), doSendSmsCaptcha())
});

 
